﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

public static class TutorialLogicUtils
{

    public static CustomBuildGoBehaviour GetBuildIngWithBuildingID(int buildingID)
    {
        var data = Building.BuildingManager.Instance.GetSelfBuildingbyConfigId(buildingID);
        if (data == null)
        {
            Debug.LogError("Building id not find is Game, Id is :" + buildingID);
            return null;
        }
        return CustomBuildManager.Instance.GetGobehaivour(data.buildingID);
    }
    public static Vector3 GetBuildingUIPos(int buildingID)
    {
        var buildingObj = GetBuildIngWithBuildingID(buildingID);
        Vector3 pos = Camera.main.WorldToScreenPoint(buildingObj.position);
       
        return GetUIPostionWithWorldPostion(pos);
    }

    public static Vector3 GetUIPosByAreaIndex(Rect areRect)
    {
        Vector2 pos = MapAreaManager.Instance.UserArea.GetCenterPosByStartGrid((int)areRect.x, (int)areRect.y, (int)areRect.width, (int)areRect.height);
        return GetUIPostionWithWorldPostion(Camera.main.WorldToScreenPoint( new Vector3( pos.x, 0, pos.y)));
    }

    public static Vector3 GetUIPostionWithWorldPostion(Vector3 pos)
    {
        Vector3 retPos = Vector3.zero;
        Canvas canvs = Hope.UIManager.Instance.UIRoot.GetComponent<Canvas>();
        RectTransform canvasTran = canvs.transform as RectTransform;
        switch (canvs.renderMode)
        {
            case RenderMode.ScreenSpaceOverlay:
                retPos = new Vector3(pos.x - canvasTran.sizeDelta.x / 2, pos.y - canvasTran.sizeDelta.y / 2, 0);
                break;
            case RenderMode.ScreenSpaceCamera:
                break;
            case RenderMode.WorldSpace:
                break;
            default:
                break;
        }
        return retPos;
    }


    public static Transform GetTransfromByUIPath(Transform root, List<string> uiPath)
    {
        string parentPath, parentName;
        UITools.GetResourcePath(uiPath[0], out parentPath, out parentName);
        var parent = Hope.UIManager.Instance.UIRoot.Find(parentName);
        if(parent == null)
        {
            Debug.LogErrorFormat("UI: {0} is not Open, Must keep UI Open To Move!", parentName);
            return null;
        }
        string childPath = UITools.GetTutorialChildPath(uiPath[1]);
        var child = parent.Find(childPath);
        if (child == null)
        {
            Debug.LogErrorFormat("UI: {0} ChildPath:{1} is Null, Please Check the Script", parentName, childPath);
            return null;
        }
        return child; 
    }
}

